class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    # 返回从尾部到头部的列表值序列，例如[1,2,3]
    def printListFromTailToHead(self, lists):
        # write code here
        if not lists:
            return []
        p = lists
        h = ListNode(0)
        h.next = lists
        p = p.next
        lists.next = None
        while p:
            l = p.next
            p.next = h.next
            h.next = p
            p = l
        return h.next


p1 = ListNode(1)
p2 = ListNode(2)
p3 = ListNode(3)
p1.next = p2
p2.next = p3
s = Solution()
m = s.printListFromTailToHead(p1)
while m:
    print(m.val)
    m = m.next

